package com.taobao.tbk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.tbk.dao.JHSItemDo;
import com.taobao.tbk.dao.JPItemDo;
import com.taobao.tbk.error.ErrorEnum;
import com.taobao.tbk.exception.CommonException;
import com.taobao.tbk.service.TbkApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/api")
public class ApiController extends BaseController {

    @Autowired
    private TbkApiService tbkApiService;

    /**
     * 好券清单接口
     */
    @RequestMapping("/item/coupon")
    @ResponseBody
    public String getTbkDgItemCoupon(@RequestParam(name = "platform_id") Long platformId,
                                     @RequestParam(name = "cat_id") String catId,
                                     @RequestParam(name = "q") String q,
                                     @RequestParam(name = "page_no") Long pageNo) throws ApiException, CommonException {
        String tbkCouponStr = this.tbkApiService.requestTbkDgItem(platformId, catId, q, pageNo);
        if (tbkCouponStr == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        return tbkCouponStr;
    }

    /**
     * 通用物料接口
     */
    @RequestMapping("/item/material")
    @ResponseBody
    public String getTbkDgMaterialOptional(@RequestParam(name = "platform_id") Long platformId,
                                           @RequestParam(name = "q") String q,
                                           @RequestParam(name = "page_no") Long pageNo,
                                           @RequestParam(name = "has_coupon") Boolean hasCoupon,
                                           @RequestParam(name = "is_tmall") Boolean isTmall,
                                           @RequestParam(name = "start_price") Long startPrice,
                                           @RequestParam(name = "end_price") Long endPrice,
                                           @RequestParam(name = "sort") String sort) throws ApiException, CommonException {
        String result = this.tbkApiService.requestTbkDgMaterialOptional(platformId, pageNo, q, hasCoupon, isTmall, startPrice, endPrice, sort);
        if (result == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        return result;
    }


    /**
     * 商品详情获取
     */
    @RequestMapping("/item/info")
    @ResponseBody
    public JSONObject getTbkItemInfo(@RequestParam(name = "item_id") String itemId) throws CommonException {
        JSONObject result = this.tbkApiService.getTbkItemInfo(itemId);
        if (result == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        return result;
    }

    /**
     * 商品淘口令生成
     * @param text
     * @param url
     * @return
     * @throws ApiException
     * @throws CommonException
     */
    @RequestMapping("/item/tpwd")
    @ResponseBody
    public JSONObject getTbkTpwdCreate(@RequestParam(name = "text") String text,
                                   @RequestParam(name = "url") String url,
                                   @RequestParam(name = "logo") String logo,
                                   @RequestParam(name = "item_id") String itemId) throws ApiException, CommonException {
        JSONObject result = this.tbkApiService.requestTbkTpwdCreate(itemId, text, url, logo);
        if (result == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        return result;
    }

    /**
     * 直接获取商品淘口令
     * @param q     要查询的关键词
     * @return
     * @throws ApiException
     * @throws CommonException
     */
    @RequestMapping("/item/matpwd")
    @ResponseBody
    public JSONObject getDgMaterialTpwd(@RequestParam(name = "q") String q) throws ApiException, CommonException {
        JSONObject resObj = this.tbkApiService.requestDgMaterialTpwd(q);
        if (resObj == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        return resObj;
    }

    /**
     * 获取精品产品列表
     * @param pageNo
     * @return
     * @throws CommonException
     */
    @RequestMapping("/item/jpitems")
    @ResponseBody
    public JSONObject getJPItemsJson(@RequestParam(name = "pageNo") Integer pageNo) throws CommonException {
        List<JPItemDo> jpItemDoList = this.tbkApiService.getJPItems(pageNo);
        if (jpItemDoList == null || jpItemDoList.size() == 0) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        String jsonStr = JSON.toJSONString(jpItemDoList);
        JSONArray jsonArr = JSONArray.parseArray(jsonStr);
        JSONObject jpItemsJsonObj = new JSONObject();
        jpItemsJsonObj.put("jp_items", jsonArr);
        return jpItemsJsonObj;
    }


    /**
     * 获取精品产品
     * @param itemId
     * @return
     * @throws CommonException
     */
    @RequestMapping("/item/jpitem")
    @ResponseBody
    public JSONObject getJPItemJson(@RequestParam(name = "itemId") String itemId) throws CommonException {
        JPItemDo jpItem = this.tbkApiService.getJPItem(itemId);
        if (jpItem == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        JSONObject jpItemJson = JSON.parseObject(JSON.toJSONString(jpItem));
        return jpItemJson;
    }

    /**
     * 获取聚划算商品列表
     * @param pageNo    页码
     * @return
     * @throws CommonException
     */
    @RequestMapping("/item/jhsitems")
    @ResponseBody
    public JSONObject getJHSItemsJson(@RequestParam(name = "pageNo") Integer pageNo) throws CommonException {
        List<JHSItemDo> jhsItemDoList = this.tbkApiService.getJHSItems(pageNo);
        if (jhsItemDoList == null || jhsItemDoList.size() == 0) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        String jsonStr = JSON.toJSONString(jhsItemDoList);
        JSONArray jsonArr = JSONArray.parseArray(jsonStr);
        JSONObject jhsItemsJsonObj = new JSONObject();
        jhsItemsJsonObj.put("jhs_items", jsonArr);
        return jhsItemsJsonObj;
    }


    /**
     * 获取聚划算产品
     * @param itemId
     * @return
     * @throws CommonException
     */
    @RequestMapping("/item/jhsitem")
    @ResponseBody
    public JSONObject getJHSItemJson(@RequestParam(name = "itemId") String itemId) throws CommonException {
        JHSItemDo jhsItem = this.tbkApiService.getJHSItem(itemId);
        if (jhsItem == null) {
            throw new CommonException(ErrorEnum.RESULT_IS_EMPTY);
        }
        JSONObject jhsItemJson = JSON.parseObject(JSON.toJSONString(jhsItem));
        return jhsItemJson;
    }

}
